function getAudioExtension(){
    // check for sound support
    var mp3Support,oggSupport;
    var audio = document.createElement('audio');
    if (audio.canPlayType) {
        // Currently canPlayType() returns: "", "maybe" or "probably"
        mp3Support = "" != audio.canPlayType('audio/mpeg');
        oggSupport = "" != audio.canPlayType('audio/ogg; codecs="vorbis"');
    } else {
        //The audio tag is not supported
        mp3Support = false;
        oggSupport = false;
    }
    return oggSupport?".ogg":mp3Support?".mp3":undefined;
}

var sound = {
    init: function(){
        // check for sound support
        this._extension = getAudioExtension();
    },
    _sounds: {},
    setSounds: function(sounds){
        if (!this._extension){
            return;
        }
        //clean old sounds
        for (var key in this._sounds){
            var current = this._sounds[key];
            for (var i=0; i<current.length; ++i){
                current[i].pause();
            }
        }
        this._sounds = {};
        //create new collection
        for (var key in sounds){
            var files = sounds[key];
            if (typeof files === 'string'){
                files = [files];
            }
            this._sounds[key] = [];
            for (var i=0; i < files.length; ++i){
                this._sounds[key].push(loader.loadAudio(files[i] + this._extension));
            }
        }
    },
    play: function(soundName){
        if (!this._extension){
            return; // no audio support
        }
        //TODO: add playing the same sound in paralel
        var sound = randomArrayElement(this._sounds[soundName])
        sound.loop=false;
        sound.play();
    },
    playLoop: function(soundName){
        if (!this._extension){
            return; // no audio support
        }
        var sound = randomArrayElement(this._sounds[soundName]); 
        sound.loop = true;
        sound.play();
    },
    stopLoop: function(soundName){
        if (!this._extension){
            return; // no audio support
        }
        var sounds = this._sounds[soundName];
        for (var i = 0; i<sounds.length; ++i){
            sounds[i].pause();
            sounds[i].currentTime = 0;
        }
    }
};

var music = {
    init: function(){
        this._extension = getAudioExtension();
    },
    setSongs: function( urls){
        this._songs = [];
        //don't preload all songs, just play what you have
        for(var i = 0; i< urls.length; ++i){
            var audio = new Audio();
            audio.src = urls[i] + sound._extension;
            audio.addEventListener('ended', function(){
                that._playNext();
            });
            this._songs.push(audio);
        }
    },
    play: function(){
        if (!this._extension){
            return; // no audio support
        }
        if (this._current){
            return; // already playing
        }
        this._current = randomArrayElement(this._songs);
        this._current.play();
    },
    stop: function(){
        if (!this._extension){
            return; // no audio support
        }
        if (!this._current){
            return // not playing
        }
        this._current.pause();
        this._current.currentTime = 0;
        this._current = null;
    },
    _playNext: function(audio){
        if (this._songs.length == 1){
            this._current.currentTime = 0;
            this._current.play();
        } else {
            var newSong;
            do{
                newSong = randomArrayElement(this._songs);
            } while (newSong == this._current);
            this._current.currentTime = 0;
            this._current = newSong;
            this._current.play();
        }
    },
    _current : null,
    _songs : []

};